<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<%
	String path = request.getContextPath();
%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>SP Code Percent List</title>
		
		<script src="<%=path%>/static/js/jquery-1.6.1.min.js" type="text/javascript"></script>
		<script src="<spring:url value="/static/js/jquery.easyui.min.js" />" type="text/javascript" ></script>
		<link rel="stylesheet" type="text/css" href="<%=path%>/static/css/themes/default/easyui.css">
		<link href="<%=path%>/static/css/themes/icon.css" rel="stylesheet" type="text/css" />
		

	<script type="text/javascript">
	
	
	function getCheckAndSubmit(percentId){
		$.messager.progress({
            title:'Please wait',
            msg:'Data is processing ...',
            text:"Loading..."
        });
	    var rows = $('#infoListTable').datagrid('getRows');
	    var valueArray = new Array();
	    $.each(rows, function(index, item){
	        if(item.percentage==''||item.percentage==null||item.percentage=='null'||item.percentage==undefined){
	        	item.percentage = 0;
		    }
	    	valueArray.push({percentId:item.percentId,providerId:item.providerId,provinceCode:item.provinceCode,spcodeId:item.spcodeId,spName:item.spName,spCodeName:item.spCodeName,paymethodName:item.paymethodName,price:item.price,dayMax:item.dayMax,max2:item.max2,percentage:item.percentage});
	    });
	    
	    $.ajax({
            type: "POST",
            url: "<%=path%>/spcodepercent/saveSpCodePercentList",
            data: JSON.stringify(valueArray),//将对象序列化成JSON字符串  
            contentType : 'application/json;charset=utf-8', //设置请求头信息  
            dataType: 'json',
            success: function(result) {
            	$.messager.progress('close');
				$('#infoListTable').datagrid('reload');
				$.messager.alert('提示', result.mes, 'info');
            }
        });
	    
	}
	
	
	function saveOrUpdate(){
		var r = $('#infoForm').form('validate');
		if (!r) {
			return false;
		}
		$.messager.progress({
            title:'Please wait',
            msg:'Data is processing ...',
            text:"Loading..."
        });
		$.post("saveOrUpdate", $("#infoForm").serializeArray(),
				function(data) {
					$('#MyPopWindow').window('close');
					$.messager.progress('close');
					$('#infoListTable').datagrid('reload');
					$.messager.alert('提示', data.mes, 'info');
				});
	}
	
			jQuery(function($){
				$('#infoListTable').datagrid({
					title:'SP Code Percent List',
					method:'post',
					iconCls:'icon-edit',
					fit:true,
					fitColumns: true,
					striped: true,
					singleSelect: true,
					url:"list",
					idField:'percentId',
					rownumbers:true,
					checkOnSelect:false,
					columns:[[
                            {field:'percentId',title:'ID',width:5,hidden:true},
                            {field:'providerId',title:'',width:5,hidden:true},
                            {field:'provinceCode',title:'',width:5,hidden:true},
                            {field:'spcodeId',title:'',width:5,hidden:true},
                            {field:'spName',title:'SP名称',width:15},
                            {field:'spCodeName',title:'业务代码',width:15},
							{field:'paymethodName',title:'业务类型',width:15},
                            {field:'provinceName',title:'省份名称',width:10},
                            {field:'providerName',title:'运营商名称',width:10},
							{field:'price',title:'价格',width:10},
							{field:'dayMax',title:'全局日限',width:10},
							{field:'max2',title:'省份日限',width:10},
							{field:'percentage',title:'流量比例',width:10, 
								editor: {
									 type: 'numberbox',
									 required:true,
						                options: {
						                    min: 0,
						                    max: 100,
						                    precision: 0
						                         }
							              }
				               
                              }
					         ]],
					         
					        
		                   onLoadSuccess : function(data) {
									if (data) {
										$('#infoListTable').datagrid('clearSelections');
										$('#infoListTable').datagrid('clearChecked');
									}
								},

								onClickCell : function(rowIndex, field, value) {

									var endEditing = function() {
										if (editIndex == undefined) {
											return true
										}
										if ($('#infoListTable').datagrid(
												'validateRow', editIndex)) {
											var ed = $('#infoListTable')
													.datagrid('getEditor', {
														index : editIndex,
														field : 'percentage'
													});
											var number = $(ed.target)
													.numberbox('getValue');
											$('#infoListTable').datagrid(
													'getRows')[editIndex]['percentage'] = number;
											$('#infoListTable').datagrid(
													'endEdit', editIndex);
											//$('#dg').datagrid('selectRow', editIndex);
											editIndex = undefined;
											return true;
										} else {
											return false;
										}
									}

									var editIndex = undefined;
									if (field != 'percentage')
										return;

									if (rowIndex != editIndex) {
										if (endEditing()) {
											$('#infoListTable').datagrid(
													'beginEdit', rowIndex);
											editIndex = rowIndex;
											var ed = $('#infoListTable')
													.datagrid('getEditor', {
														index : rowIndex,
														field : 'percentage'
													});
											$(ed.target).focus().bind('blur',
													function() {
														endEditing();
													});
										} else {
											//$('#dg').datagrid('selectRow', editIndex);
											editIndex = undefined;
										}
									}

								}

							});
		});

		$.ajaxSetup({
			global : false,
			type : "POST",
			complete : function(XMLHttpRequest, textStatus) {
				var data = XMLHttpRequest.responseText;
				var redirect = window.top.location.host + "/YouLeCloud/login";
				if (data == "timeout") {
					if (window.top != window.self) {
						window.top.location.href = "${redirect}";
						return;
					}
				}
			}
		});

		function addrow() {
			$("#MyPopWindow").window({
				title : '增加信息',
				href : 'popWindow',
				width : 400,
				height : 220,
				onLoad : function() {
					//$('#form').form('clear');
				}

			});
		}

		function updaterow() {
			var rows = $('#infoListTable').datagrid('getSelections');
			if (rows.length == 0) {
				$.messager.alert('提示', "请选择你要更新的信息", 'info');
				return;
			}
			if (rows.length > 1) {
				$.messager.alert('提示', "只能选择一位进行更新", 'info');
				return;
			}
			$("#MyPopWindow").window({
				title : '更新信息',
				href : 'popWindow',
				width : 400,
				height : 220,
				onLoad : function() {
					$("#infoForm").form('load', rows[0]);
				}
			});
		}

		function reloadOrderDataGrid() {
			var params = $('#infoListTable').datagrid('options').queryParams;
			var fields = $('#queryForm').serializeArray();
			$.each(fields, function(i, field) {
				params[field.name] = field.value;
			});
			$('#infoListTable').datagrid('reload');
		}

		var height = window.screen.height - 250;
		var width = window.screen.width;
		var leftW = 300;
		if (width > 1200) {
			leftW = 400;
		} else if (width > 1000) {
			leftW = 350;
		} else {
			leftW = 100;
		}

		function openViewFrame(o) {
			$('#idDetailView').html("");
			$('#idDetailView').window('open');

			$('#idDetailView')
					.append(
							"<div id='idDivWait' style='position:absolute;left:0;width:100%;height:"
									+ height
									+ "px;top:20px;background:#E0ECFF;opacity:0.8;filter:alpha(opacity=80);'>"
									+ "<div style='position:absolute;  cursor1:wait;left:40%; top:40%;width:auto;height:16px;padding:12px 5px 10px 30px;"
									+ "background:#fff url(../static/css/themes/default/images/loading.gif) no-repeat scroll 5px 10px;border:2px solid #ccc;color:#000;'>"
									+ "Loading... Please wait..."
									+ "</div></div>");

			$('#idDetailView')
					.append(
							'<iframe id="idDetailViewiFrame" frameborder="0" style="width:100%;height:100%;"></iframe>');

			$('#idDetailViewiFrame').attr("src", $(o).attr("srcurl"));
		}

		function reloadDataGrid() {
			var params = $('#infoListTable').datagrid('options').queryParams;
			var fields = $('#queryForm').serializeArray();
			$.each(fields, function(i, field) {
				params[field.name] = field.value;
			});
			$('#infoListTable').datagrid('reload');
		}

		function searchInfo() {
			reloadDataGrid();
		}
	</script>
		
	</head>


	<body class="easyui-layout" style="height: 100%">
	
		<div data-options="region:'north'" 
			style="padding: 1px 0 1px 1px; height: 80px;"
			title="查询">
			<form id="queryForm" style="margin:5;text-align: center;">
			<table width="100%">
				<tr>
					<td>
						省份 &nbsp;&nbsp;&nbsp;&nbsp;<input class="easyui-combobox" 
							name="provinceName"
							data-options="
									url:'<%=path%>/spcodepercent/spCodePercentprovinceCodeListAll',
									method:'get',
									valueField:'name',
									textField:'name',
									hasDownArrow :true,
									panelHeight:'200'
							">
					<td>
						运营商 &nbsp;&nbsp;&nbsp;&nbsp;<input class="easyui-combobox" 
							name="providerName"
							data-options="
									url:'<%=path%>/spcodepercent/spCodePercentproviderIDListAll',
									method:'get',
									valueField:'name',
									textField:'name',
									hasDownArrow :true,
									panelHeight:'auto'
							">
					</td>
					<td>
						<a href="#" onclick="searchInfo();" class="easyui-linkbutton" iconCls="icon-search"><div style="width: 30px;float: left;">查询</div></a>
						
					</td>
					
					<td>
					<a href="#" id="ButonGetCheck" onclick="getCheckAndSubmit();" class="easyui-linkbutton" iconCls="icon-save"><div style="width: 30px;float: left;">保存</div></a>
					</td>
				</tr>
			</table>
			</form>
		</div>
	
	
		<div data-options="region:'center'" style="padding:10; " id="tabdiv">
			<table id="infoListTable"></table>
		</div>
		
		<div id="idDetailView" title="Detail View"  class="easyui-window" data-options="closed:true,iconCls:'icon-save'" style="width:755px;height:440px;">
	 	</div> 
	 	<div id="MyPopWindow" modal="true" shadow="false" minimizable="false" cache="false" maximizable="false" collapsible="false" resizable="false" style="margin: 0px;padding: 0px;overflow: auto;"></div>
	</body>
